#!/usr/bin/env python2
# GoodFET Debugger
# 
# (C) 2009 Travis Goodspeed <travis at radiantmachines.com>
#
# This code is being rewritten and refactored.  You've been warned!

import sys, os, code, binascii;
#import rlcompleter, readline;

from GoodFET import GoodFET, getClient;
from GoodFETConsole import GoodFETConsole;

from intelhex import IntelHex;

if(len(sys.argv)==1):
    print "Usage: %s [driver|verb]\n" % sys.argv[0];
    print "driver:= monitor | cc | avr | spi | msp430 | nrf";
    print "verb:=   scan";
    print "";
    print "This is an unfinished client.  You probably want goodfet.$chip instead.";
    sys.exit(1);

verb=sys.argv[1];
if verb=="scan":
    from scanwin32 import *;
    # INIT COMPORT SCAN
    scan=winScan();
    #scan.comports();
    
    for order, port, desc, hwid in sorted(scan.comports()):
        # Look for FTDIBUS
        try:
            #hwid.index('FTDI')
            #print "*************"
            #print "GOODFET FOUND"
            #print "*************"
            if hwid.index('FTDI')!=0: continue;
            print "%s: (%s)" % (port, hwid),
            try:
                serial.Serial(port)
            except serial.serialutil.SerialException:
                print "Busy"
            else:
                print "Ready"
        except:
            pass
    sys.exit(0);

driver=sys.argv[1];
print "Using driver %s" % driver;
#client=eval("%s()" % driver);
client=getClient(driver);
console=client.getConsole();
console.run();

sys.exit(0);
